package com.tushen.datacenter.ts.adpter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.util.ImageUtil;
import com.tushen.datacenter.ts.R;
import com.tushen.datacenter.ts.JinChuStatusBean;

import java.util.List;

public class StatusAdapter extends ArrayAdapter<JinChuStatusBean> {
    private int resourceId;
    private Context context;

    // 适配器的构造函数，把要适配的数据传入这里
    public StatusAdapter(Context ctx, int textViewResourceId, List<JinChuStatusBean> objects){
        super(ctx,textViewResourceId,objects);
        resourceId=textViewResourceId;
        context = ctx;
    }

    // convertView 参数用于将之前加载好的布局进行缓存
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        JinChuStatusBean bean= getItem(position); //获取当前项的Fruit实例

        // 加个判断，以免ListView每次滚动时都要重新加载布局，以提高运行效率
        View view;
        ViewHolder viewHolder;
        if (convertView==null){

            // 避免ListView每次滚动时都要重新加载布局，以提高运行效率
            view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false);

            // 避免每次调用getView()时都要重新获取控件实例
            viewHolder=new ViewHolder();
            viewHolder.image=view.findViewById(R.id.iv_image);
            viewHolder.name=view.findViewById(R.id.tv_name);
            viewHolder.banzu=view.findViewById(R.id.tv_banzu);
            viewHolder.gongzhong=view.findViewById(R.id.tv_gongzhong);
            viewHolder.time=view.findViewById(R.id.tv_time);
            viewHolder.company=view.findViewById(R.id.tv_company);
            viewHolder.location=view.findViewById(R.id.tv_location);
            ImageUtil.getInstance().displayImage(context,bean.faceImgPath,view.findViewById(R.id.iv_image), R.drawable.blank);


            // 将ViewHolder存储在View中（即将控件的实例存储在其中）
            view.setTag(viewHolder);
        } else{
            view=convertView;
            viewHolder=(ViewHolder) view.getTag();
        }

        // 获取控件实例，并调用set...方法使其显示出来
        //viewHolder.image.setImageResource(bean.imageId);
        ImageUtil.getInstance().displayImage(context,bean.faceImgPath,view.findViewById(R.id.iv_image), R.drawable.blank);
        viewHolder.name.setText(bean.name);
        viewHolder.banzu.setText(bean.banzu);
        viewHolder.gongzhong.setText(bean.gongzhong);
        viewHolder.company.setText(bean.company);
        viewHolder.company.setSelected(true);
        viewHolder.time.setText(bean.time);
        viewHolder.location.setText(bean.location);

        return view;
    }

    // 定义一个内部类，用于对控件的实例进行缓存
    class ViewHolder{
        ImageView image;
        TextView name;
        TextView banzu;
        TextView gongzhong;
        TextView time;
        TextView company;
        TextView location;

    }
}
